package com.jydw.da.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.jydw.da.common.core.dataModel.common.vo.ResponseBean;
import com.jydw.da.frame.mapper.SysMenuMapper;
import com.jydw.da.frame.pojo.SysMenu;
import com.jydw.da.frame.service.SysMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 系统菜单服务impl
 *
 * @author chenyj
 * @date 2023/06/19
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    SysMenuMapper sysMenuMapper;

    @Override
    public ResponseBean<?> getTree() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", 0);
        // 父菜单
        List<SysMenu> parents = sysMenuMapper.selectList(wrapper);
        // 子菜单
        QueryWrapper<SysMenu> wrapperNotEqual = new QueryWrapper<>();
        wrapperNotEqual.ne("parent_id", 0);
        List<SysMenu> children = sysMenuMapper.selectList(wrapperNotEqual);
        parents.forEach(parent -> {
            List<SysMenu> childrenNode = existsNode(parent, children);
            parent.setMenuChildren(childrenNode);
        });
        return ResponseBean.success(parents);
    }

    private List<SysMenu> existsNode(SysMenu parent, List<SysMenu> childrenNode) {
        List<SysMenu> dictList = Lists.newArrayList();
        childrenNode.forEach(child -> {
            Long parentId = child.getParentId();
            if (parent.getMenuId().equals(parentId)) {
                List<SysMenu> grandSonNode = existsNode(child, childrenNode);
                child.setMenuChildren(grandSonNode);
                dictList.add(child);
            }
        });
        return dictList;
    }

    public static void main(String[] args) {
        Integer a= 1;
        System.out.println(UUID.randomUUID().hashCode() %4);
        System.out.println(a.hashCode());
    }
}
